﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.InfoCardListRequest
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;

namespace Microsoft.InfoCards
{
  internal class InfoCardListRequest : UIAgentRequest
  {
    private InfoCardPolicy m_policy;
    private IList<InfoCard> m_allCards;

    public InfoCardListRequest(
      IntPtr rpcHandle,
      Stream inArgs,
      Stream outArgs,
      ClientUIRequest parent)
      : base(rpcHandle, inArgs, outArgs, parent)
    {
    }

    protected override void OnInitializeAsSystem()
    {
      base.OnInitializeAsSystem();
      this.m_policy = this.GetPolicy();
    }

    protected override void OnMarshalInArgs()
    {
    }

    protected override void OnProcess()
    {
      IList<DataRow> allCards = this.GetAllCards();
      if (allCards == null || allCards.Count == 0)
        return;
      IList<InfoCard> infoCardList = (IList<InfoCard>) new List<InfoCard>(allCards.Count);
      for (int index = 0; index < allCards.Count; ++index)
      {
        byte[] dataField = allCards[index].GetDataField();
        try
        {
          using (MemoryStream memoryStream = new MemoryStream(dataField))
            infoCardList.Add(new InfoCard((Stream) memoryStream));
        }
        finally
        {
          Array.Clear((Array) dataField, 0, dataField.Length);
        }
      }
      this.m_allCards = infoCardList;
    }

    protected override void OnMarshalOutArgs()
    {
      Stream outArgs = this.OutArgs;
      BinaryWriter binaryWriter = new BinaryWriter(outArgs);
      if (this.m_allCards != null && this.m_allCards.Count != 0)
      {
        StoreConnection connection = StoreConnection.GetConnection();
        try
        {
          binaryWriter.Write(this.m_allCards.Count);
          for (int index = 0; index < this.m_allCards.Count; ++index)
          {
            binaryWriter.Flush();
            this.m_allCards[index].AgentSerialize(outArgs, this.ParentRequest is GetTokenRequest, this.m_policy, connection, new CultureInfo(this.ParentRequest.UserLanguage));
          }
          binaryWriter.Flush();
        }
        finally
        {
          if (this.m_allCards != null)
          {
            foreach (InfoCard allCard in (IEnumerable<InfoCard>) this.m_allCards)
              allCard.ClearSensitiveData();
          }
          connection.Close();
        }
      }
      else
        binaryWriter.Write(0);
    }

    private IList<DataRow> GetAllCards()
    {
      StoreConnection connection = StoreConnection.GetConnection();
      try
      {
        return (IList<DataRow>) connection.Query(QueryDetails.FullRow, connection.LocalDataSource, new QueryParameter("ix_objecttype", new object[1]
        {
          (object) 1
        }));
      }
      finally
      {
        connection.Close();
      }
    }
  }
}
